import PageTemplate from '@/components/common/PageTemplate'
import api_pur from '@/api/api_pur'
import {getDraftListByPaging,delMyDraft} from '@/api/api'
import api_home from '@/api/api_home'
import * as Constant from '@/utils/constant'
import store from '@/store'

export default {
    components:{
      PageTemplate
    },
    data() {
        return {
            brand:[
                {name:'lang.workbench'},
                {name:'lang.mydraft'}
              ],
            hasIcon:false,
            advSearch:'te.advance_search',
            currentPage:1,
            pageSize:10,
            total:0,
            showCollapse:'showCollapse',
            mydraftList:[],
            keyword:'',
            launchTime:null,
            folio:'',
            searchParams:{},
            processRouterMap: {
              "SWP-SU\\pur_demIn": Constant.ROUTE_PUR_PROCESS_DEM_WITHIN.index,
              "SWP-SU\\pur_demOut": Constant.ROUTE_PUR_PROCESS_DEM_WITHOUT.index,
              "SWP-SU\\pur_demChange": Constant.ROUTE_PUR_PROCESS_DEM_CHANGE.index,
              "SWP-SU\\pur_demClose": Constant.ROUTE_PUR_PROCESS_DEM_CLOSE.index,
              "SWP-SU\\sci_thesis": Constant.ROUTE_SRI_GAIN_THESIS_DRAFTEDIT.index,
              "SWP-SU\\sci_cbpPlan": Constant.ROUTE_SRI_GAIN_CBP_PLAN_DRAFTEDIT.index,
              "SWP-SU\\sci_cbpProcess": Constant.ROUTE_SRI_GAIN_CBP_PROGRESS_DRAFTEDIT.index,
              "SWP-SU\\sri_alpPlan": Constant.ROUTE_SRI_GAIN_ALP_PLAN_DRAFTEDIT.index,
              "SWP-SU\\sri_alpProcess": Constant.ROUTE_SRI_GAIN_ALP_PROGRESS_DRAFTEDIT.index,
              "SWP-SU\\sci_budget": Constant.ROUTE_SRI_BUDGET_DRAFTEDIT.index,
              "SWP-SU\\hr_frontApproval": Constant.ROUTE_HR_FONT_DRAFTEDIT.index,
              "SWP-SU\\hr_backApproval": Constant.ROUTE_HR_BACK_DRAFTEDIT.index,
            },
            delReqMap:{
                "SWP-SU\\sci_thesis": "deleteDraftPaper",
                "SWP-SU\\sci_cbpPlan": "deleteDraftCapacityBuildingPlan",
                "SWP-SU\\sci_cbpProcess": "deleteDraftCapacityBuildingProcess",
                "SWP-SU\\sri_alpPlan": "deleteDraftLaboratoryAnnualPlan",
                "SWP-SU\\sri_alpProcess": "deleteDraftLaboratoryAnnualProcess",
                "SWP-SU\\sci_budget": "deleteDraftBudget",
                "SWP-SU\\hr_frontApproval": "deleteDraftHr",
                "SWP-SU\\hr_backApproval": "deleteDraftHr",
            }
        }
    },
    methods: {
        showList(params){
            let _this = this;
            getDraftListByPaging(params).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let list = result.data.data.data.resultData;
                    _this.mydraftList = list;
                    _this.currentPage = result.data.data.data.startPage;
                    _this.pageSize = result.data.data.data.pageSize;
                    _this.total = result.data.data.data.totalCount;
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        changeSize(size){//改变每页显示数
            let params = {pageIndex:1,pageSize:size,procName:store.state.proc.procName};
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        },
        changeCurrentPage(index){//改变页数
            let params = {pageIndex:index,pageSize:this.pageSize,procName:store.state.proc.procName};
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        },
        prePage(index){//上一页
            let params = {pageIndex:index,pageSize:this.pageSize,procName:store.state.proc.procName};
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        },
        nextPage(index){//下一页
            let params = {pageIndex:index,pageSize:this.pageSize,procName:store.state.proc.procName};
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        },
        //流程初始化
        initList(){
            let params = {
                pageIndex:1,
                pageSize:this.pageSize,
                procName:store.state.proc.procName,
            }
            this.showList(params);
        },
        showDealer(obj){
            //显示待处理人
            let userIds = obj.account.split(",");
            let userNames = obj.userchsname.split(",");
            let users = [];
            for(let i = 0; i < userIds.length; i++){
                users.push("["+userIds[i]+"]"+userNames[i]);
            }
            return users;
        },
        //删除草稿
        delDraft:function(row){
            let _this = this;
            this.$confirm(_this.$t('dft.isDeleted'),_this.$t('cm.tips'),{
                type:'error',
                confirmButtonText: this.$t('te.confirm'),
                cancelButtonText: this.$t('te.cancel'),
                cancelButtonClass: 'btn-second',
                confirmButtonClass: 'btn-default',
            }).then(()=>{
                // delMyDraft({id:row.id,formNo:row.formNo}).then((result) => {
                //     if(result.status == "200" && result.data.code == "0"){
                //         _this.initList();
                //     }else{
                //         _this.$alert(result.data.msg,_this.$t('cm.tips'));
                //     }
                // }).catch((err) => {
                //     _this.$alert(err,_this.$t('cm.tips'));
                // });
                let req = this.delReqMap[row.processfullname];
                if(req){
                    api_pur[req]({id:row.id,formno:row.formno},result=>{
                        if (result.code === Constant.RT_SUCCESS) {
                            _this.initList();
                        } else {
                            ALERT.error(result.msg)
                        }

                    })

                }else{
                    api_pur.purContractItemProcessDel({id: row.id}, result => {
                    if (result.code === Constant.RT_SUCCESS) {
                        _this.initList();
                    } else {
                        // _this.$t('cm.tips')
                        ALERT.error(result.msg)
                    }
                    })
                }
            })
            
        },
        //跳转的我的草稿详情，准备修改或者提交
        editDraft:function(item){
            let _this = this;
            console.log('item')
            console.log(item)
            if(this.processRouterMap[item.processfullname]) {
              api_home.updateVisitTime({processFullName: item.processFullName, processName: item.processName}, response => {
              })
              setTimeout(()=>{
                _this.$router.push({path:this.processRouterMap[item.processfullname],query:{obj:item}});
              }, 200)
            }else{
                _this.$alert("please check processRouterMap.");
            }
            // this.$router.push({path:"/mydraft_edit",query:{obj:row}});
        },
        //高级搜索展开
        advanceSearch: function(){
            if(this.advSearch == "te.advance_search"){
                this.advSearch = "te.return";
            }else{
                this.advSearch = "te.advance_search";
            }
        },
        //重置操作
        reset:function(){
            this.launchTime = null;
            this.folio = '';
        },
        //高级搜索
        advSearchs:function(){
            this.searchParams = {
                launchTime: this.launchTime,
                folio: this.folio
            };
            let params = {
                pageIndex:1,
                pageSize:this.pageSize,
                procName:store.state.proc.procName,
            }
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        },
        commonSearch:function(){
            this.searchParams = {
                folio: this.keyword
            };
            let params = {
                pageIndex:1,
                pageSize:this.pageSize,
                procName:store.state.proc.procName,
            }
            params = Object.assign(params,this.searchParams);
            this.showList(params);
        }
    },
    mounted() {
        //这里进行查询草稿列表，在公共组件修改草稿并进行保存。
        this.initList();
    },
}